// packageG/mycomponent/assist-competition-vote/index.js
let app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: null
    },
    component_id: {
      type: null
    },
    U_tabcontrol: {
      type: null
    },
    bottomShowBolIndex: {
      type: null
    },
    index: {
      type: null
    },
    bottomShowBol: {
      type: null
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: null,
    keyword: '',
    isLoadMore: false,
    page: 1,
    total_page: 0,
    allLoaded: false, // 全部数据已经加载完成
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.initData();
      this.getData();
      this.setData({
        diyStyle: `
          --preview_color: ${this.data.datas.preview_color};
          --seach_txt_color: ${this.data.datas.seach_txt_color};
          --seach_btn_txt_color: ${this.data.datas.seach_btn_txt_color};
          --box_color: ${this.data.datas.box_color};
          --border_color: ${this.data.datas.border_color};
          --box_name_color: ${this.data.datas.box_name_color};
          --box_hot_color: ${this.data.datas.box_hot_color};
          --vote_btn_bg: ${this.data.datas.vote_btn_bg};
          --btn_text_color: ${this.data.datas.btn_text_color};
          --seach_bg_color: ${this.data.datas.seach_bg_color};
        `
      });
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  observers: {
    bottomShowBol: function (flag) {
      console.log(flag);
      if(this.data.childComponent != 'tab'){
        if (this.data.U_tabcontrol) {
          // 选项卡this.isLast判断商品列表是最后一个组件
          if (flag && this.data.isLast && this.data.isLoadMore) {
            this.getMoreData();
          }
        } else {
          if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.isLoadMore) {
            this.getMoreData();
          }
        }
      }
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.assist-voting.index');
      app._postNetWork({
        url: urlStr,
        data: {
          activity_id: this.data.datas.form.id,
          keyword: this.data.keyword
        },
        success: (resdata)=> {
          let {
            result,
            msg,
            data
          } = resdata.data;
    
          if (result == 0) return app.tips(msg);
          let setData = {};
          setData.list = data.data;
    
          setData.total_page = data.last_page;
          if (setData.total_page <= 1) {
            setData.isLoadMore = false;
            setData.allLoaded = true;
          } else {
            setData.isLoadMore = true;
          }
    
          this.setData(setData);
        },
        fail: function(res) {
          console.log(res);
        }
      });
      
    },
    onSearch() {
      this.initData();
      this.getData();
    },
    initData() {
      this.data.list = null;
      this.data.isLoadMore = false;
      this.data.page = 1;
      this.data.total_page = 0;
    },
    toVote(event) {
      let {
        id, activity_id
      } = event.currentTarget.dataset;
      wx.navigateTo({
        url: "/mircoApp/assist-competition/details?signup_record_id=" + id + "&activity_id=" + activity_id
      });
    },

    async getMoreData() {
      this.data.isLoadMore = false; // 防止多次请求分页数据
      if (this.data.page >= this.data.total_page) {
        console.log("getMoreData", this.data.total_page);
      } else {
        this.data.page = this.data.page + 1;

        let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.assist-voting.index');
        let response = await app._postNetWork({
          url: urlStr,
          data: {
            activity_id: this.data.datas.form.id,
            keyword: this.data.keyword,
            page: this.data.page
          }
        });
        let { result, msg, data } = response.data;
        let setData = {};

        if (result == 0) {
          setData.data.page = this.page - 1;
          setData.data.isLoadMore = false;
          setData.data.allLoaded = true;
          this.setData(setData);
          return app.tips(msg);
        }

        setData.data.total_page = data.last_page;
        if (setData.data.page < setData.data.total_page) {
          setData.data.isLoadMore = true;
        } else {
          setData.data.allLoaded = true;
        }

        setData.data.list = setData.data.list.concat(data.data);
        this.setData(setData);

        // this.$emit('setLoadMore', this.isLoadMore);
      }
    },
  }
})